package com.wxl.common.http

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.Lifecycle
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.https.HttpsUtils
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.request.GetRequest
import com.lzy.okgo.request.PostRequest
import com.wxl.common.AppConstant
import com.wxl.common.AppContext
import com.wxl.common.life.LifecycleHandler
import com.wxl.common.util.SpUtil
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.logging.Level
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 * create file time : 2021/2/22
 * create user : wxl
 * subscribe :
 */
class OkGoApi {

    private val lifecycles = HashMap<String, ArrayList<String>>()
    private val tags = HashMap<String, String>()
    private lateinit var baseUrl: String

    companion object {
        val api = OGA.oga
    }

    private object OGA {
        val oga = OkGoApi()
    }

    private constructor() {
        val token = SpUtil.sp.getString("token")
        val userId = SpUtil.sp.getString("userId")
       // val headerParams = HttpHeaders()
        //headerParams.put("token", token)
       // headerParams.put("userId", userId)

        val commonParams = HttpParams()
        commonParams.put("token",token)
        commonParams.put("userId",userId)
        commonParams.put("sign","")
        commonParams.put("timestamp",System.currentTimeMillis())
        commonParams.put("c","entry")
        commonParams.put("m","zh_tcwq")
        commonParams.put("a","phoneapp")
        commonParams.put("i","136")
        commonParams.put("from","android")

        val loggingInterceptor = HttpLoggingInterceptor("Loog")
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
        loggingInterceptor.setColorLevel(Level.INFO)

        val sslParams = HttpsUtils.getSslSocketFactory()

        OkGo.getInstance().apply {
            okHttpClient = okHttpClient.newBuilder().also {
                it.run {
                    connectTimeout(AppConstant.outTime, TimeUnit.SECONDS)
                    readTimeout(AppConstant.outTime, TimeUnit.SECONDS)
                    writeTimeout(AppConstant.outTime, TimeUnit.SECONDS)
                    addInterceptor(loggingInterceptor)
                    sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                    hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier)
                }
            }.build()
        }?.run {
            cacheMode = CacheMode.NO_CACHE
            //addCommonHeaders(headerParams)
            addCommonParams(commonParams)
        }
    }


    /**
     * 设置基础路径
     */
    fun setBaseUrl(baseUrl: String, app: Application) {
        this.baseUrl = baseUrl
        OkGo.getInstance().init(app)
    }

    /**
     * get请求
     * 跟随当前栈顶的activity生命周期结束请求
     */
    fun get(): GetRequest<Any> {
        val activity = AppContext.appContext.getCurrentActivity()
        activity?.let { return getTarget(it.javaClass) }
        return getReq()
    }

    /**
     * post请求
     * 跟随当前栈顶的activity生命周期结束请求
     */
    fun post(): PostRequest<Any> {
        val activity = AppContext.appContext.getCurrentActivity()
        activity?.let { return postTarget(it.javaClass) }
        return postReq()
    }


    /**
     * get请求
     * @param lifecycle 跟随栈顶生命周期结束请求
     */
    private fun getTarget(lifecycle: Class<Any>): GetRequest<Any> {
        val tag = lifecycle.name + System.currentTimeMillis()
        registerLifecycle(lifecycle, tag)
        return OkGo.get<Any>(baseUrl).tag(tag)
    }


    /**
     * get请求
     * @param lifecycle 跟随生命周期ONDESTROY结束请求
     */
    fun get(lifecycle: Lifecycle): GetRequest<Any> {
        val tag = UUID.randomUUID().toString() + System.currentTimeMillis()
        lifecycle.addObserver(LifecycleHandler(tag, 3))
        return OkGo.get<Any>(baseUrl).tag(tag)
    }

    /**
     * post请求
     * @param lifecycle 跟随栈顶生命周期结束请求
     */
    private fun postTarget(lifecycle: Class<Any>): PostRequest<Any> {
        val tag = lifecycle.name + System.currentTimeMillis()
        registerLifecycle(lifecycle, tag)
        return OkGo.post<Any>(baseUrl).tag(tag)
    }


    /**
     * post请求
     * @param lifecycle 跟随生命周期ONDESTROY结束请求
     */
    fun post(lifecycle: Lifecycle): PostRequest<Any> {
        val tag = UUID.randomUUID().toString() + System.currentTimeMillis()
        lifecycle.addObserver(LifecycleHandler(tag, 3))
        return OkGo.post<Any>(baseUrl).tag(tag)
    }

    /**
     * get 不受生命周期影响
     */
    fun getReq(): GetRequest<Any> {
        return OkGo.get<Any>(baseUrl)
    }

    /**
     * post 不受生命周期影响
     */
    fun postReq(): PostRequest<Any> {
        return OkGo.post(baseUrl)
    }

    /**
     * 注册生命周期监听
     */
    private fun registerLifecycle(targetCls: Class<Any>, tag: String) {
        if (!lifecycles.containsKey(targetCls.name)) {
            lifecycles[targetCls.name] = ArrayList()
        }
        lifecycles[targetCls.name]?.add(tag)
        tags[tag] = targetCls.name
    }

    /**
     * 取消
     */
    fun cancelTag(tag:String){
        tags.remove(tag)?.let { itTag ->
            lifecycles[itTag]?.let {
                it.remove(tag)
                OkGo.getInstance().cancelTag(it)
            }
        }
    }

    /**
     * 取消该类(栈顶)下的所有请求
     */
    internal fun cancelTargetApi(targetCls: Class<Any>) {
        if (lifecycles.containsKey(targetCls.name)) {
            lifecycles.remove(targetCls.name)?.let {
                for (s in it) {
                    if (!TextUtils.isEmpty(s)) {
                        OkGo.getInstance().cancelTag(s)
                    }
                }
            }
        }
    }
}